//
//  ZZEmotionKeyboardView.swift
//  web
//
//  Created by 张卓 on 2017/2/10.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

class ZZEmotionKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 刷新最近表情的UI
    func reloadRecentData(){
        //第一组  第一页
        let indexPath = IndexPath(item: 0, section: 0)
        self.emoticonPageView.reloadItems(at: [indexPath])
    }
    
    
    
    //MARK: - 设置视图
    private func setupUI(){
       //设置大小
        self.frame.size = CGSize(width: ZZSCREENW, height: 216)
        backgroundColor = UIColor(patternImage: UIImage(named:"emoticon_keyboard_background")!)
        //添加空间
        addSubview(emoticonButtonView)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        //设置约束
        emoticonButtonView.snp_makeConstraints { (make) in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(35)
        }
        
        emoticonPageView.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(emoticonButtonView.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView).offset(10)
        }
        
        //因为当前滚动的时候pageView还没有加载完成.所以无法看到效果,需要使用GCD 通过主线程异步才能解决该BUG
        DispatchQueue.main.async {
            //设置pageView默认滚到第一组
            let indexPath = IndexPath(item: 0, section: 1)
            //pageView滚动到指定的组
            self.emoticonPageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
            
            //设置pageControl
            self.setupPageControl(indexPath: indexPath)
        }
        
        //监听底部视图的点击
        emoticonButtonView.closure = {(type)->() in
            switch type {
            case .recent:
                print("最近")
            case .normal:
                print("默认")
            case .emoji:
                print("emoji")
            case .xlh:
                print("小浪花")
            }
            //indexPath
            let indexPath = IndexPath(item: 0,section: type.rawValue - 100)
            //pageview
            self.emoticonPageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
        }
        
        
    }
    
    //MARK: - 懒加载空间
    fileprivate lazy var emoticonButtonView: ZZEmoticonButtonView = ZZEmoticonButtonView()
    // 表情键盘使用的VIew
    fileprivate lazy var emoticonPageView:ZZEmoticonPageView = {
        let view = ZZEmoticonPageView()
        view.backgroundColor = self.backgroundColor
        view.delegate = self
        //取消弹簧效果
        view.bounces = false
        //设置分页
        view.isPagingEnabled = true
        //取消滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        return view
    }()
    
    //分页指示器
    fileprivate lazy var pageControl : UIPageControl = {
       let view = UIPageControl()
        view.numberOfPages = 4
        //当前页数
        view.currentPage = 1
        //设置默认图片(KVC)
        view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "pageImage")
//        view.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        //设置选中图片
        view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "currentPageImage")
//        view.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        
        view.isUserInteractionEnabled = false
        //如果UIPageControl 总页数为1 的时候不显示
        view.hidesForSinglePage = true
        return view
    }()
    
    
}

extension ZZEmotionKeyboardView: UICollectionViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //获取中心点的X
        let centerX = ZZSCREENW / 2 + scrollView.contentOffset.x
        //获取中心点的Y
        let centerY = CGFloat((216 - 35) / 2)
        //获取center
        let center = CGPoint(x: centerX, y: centerY)
        //获取屏幕中心点对应的indexpath
        if let indexPath = self.emoticonPageView.indexPathForItem(at: center) {
            //设置选择的按钮状态
            self.emoticonButtonView.setupSelectButton(tag: indexPath.section + 100)
            
            //设置pageControl
            self.setupPageControl(indexPath: indexPath)
        }
        
    }
    
}

extension ZZEmotionKeyboardView{
    //设置pagecontrol
    func setupPageControl(indexPath: IndexPath){
        //设置pageControl
        self.pageControl.numberOfPages = ZZEmoticonTools.shared.allEmoticons[indexPath.section].count
        self.pageControl.currentPage = indexPath.item
    }
    
    
}






